//
//  StayInView.swift
//  AirTravel
//
//  Created by apple on 2021/7/14.
//

import UIKit
import FWPopupView

protocol StayInDelegate:NSObjectProtocol {
    func stayInClick(data:Dictionary<String,Any>)
}

class StayInView: FWPopupView {
    var contentView:UIView!
    weak var delegate: StayInDelegate?
   
    @IBOutlet weak var jianNumberLab: UILabel!
    @IBOutlet weak var jianReductionBtn: UIButton!
    @IBOutlet weak var jianAddBtn: UIButton!
    var jianNumber = 0
    
    
    @IBOutlet weak var crNumberLab: UILabel!
    @IBOutlet weak var crReductionBtn: UIButton!
    @IBOutlet weak var crAddBtn: UIButton!
    var crNumber = 0
    
    @IBOutlet weak var erNumberLab: UILabel!
    @IBOutlet weak var erReductionBtn: UIButton!
    @IBOutlet weak var erAddbtn: UIButton!
    var erNumber = 0
    
    @IBOutlet weak var completeBtn: UIButton!
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
    
        self.completeBtn.layer.cornerRadius = 6
        self.completeBtn.layer.masksToBounds = true
        self.completeBtn.addTarget(self, action: #selector(complete), for: .touchUpInside)
        
        self.jianAddBtn.layer.borderWidth = 1
        self.jianAddBtn.layer.borderColor = UIColor(red: 255.0 / 255.0, green: 138.0 / 255.0, blue: 0.0 / 255.0, alpha: 1.0).cgColor
        self.jianAddBtn.addTarget(self, action: #selector(jianAdd), for: .touchUpInside)
        self.jianReductionBtn.addTarget(self, action: #selector(jianReduction), for: .touchUpInside)
        
        self.crAddBtn.layer.borderWidth = 1
        self.crAddBtn.layer.borderColor = UIColor(red: 255.0 / 255.0, green: 138.0 / 255.0, blue: 0.0 / 255.0, alpha: 1.0).cgColor
        self.crAddBtn.addTarget(self, action: #selector(crAdd), for: .touchUpInside)
        self.crReductionBtn.addTarget(self, action: #selector(crReduction), for: .touchUpInside)
        
        self.erAddbtn.layer.borderWidth = 1
        self.erAddbtn.layer.borderColor = UIColor(red: 255.0 / 255.0, green: 138.0 / 255.0, blue: 0.0 / 255.0, alpha: 1.0).cgColor
        self.erAddbtn.addTarget(self, action: #selector(erAdd), for: .touchUpInside)
        self.erReductionBtn.addTarget(self, action: #selector(erReduction), for: .touchUpInside)
    }

    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    //间数
    @objc func jianAdd() {
        self.jianNumber += 1
        self.jianNumberLab.text = "\(self.jianNumber)"
    }
    @objc func jianReduction() {
        if self.jianNumber > 0 {
            self.jianNumber -= 1
            self.jianNumberLab.text = "\(self.jianNumber)"
        }
        
    }
    //成人
    @objc func crAdd() {
        self.crNumber += 1
        self.crNumberLab.text = "\(self.crNumber)"
    }
    
    @objc func crReduction() {
        if crNumber > 0 {
            self.crNumber -= 1
            self.crNumberLab.text = "\(self.crNumber)"
        }
       
    }
    //儿童
    @objc func erAdd() {
        self.erNumber += 1
        self.erNumberLab.text = "\(self.erNumber)"
    }
    
    @objc func erReduction() {
        if erNumber > 0 {
            self.erNumber -= 1
            self.erNumberLab.text = "\(self.erNumber)"
        }
        
    }
    
    @IBAction func back(_ sender: Any) {
        self.hide()
        self.hideKeyboard()
    }
    
    @objc func complete() {
        self.delegate?.stayInClick(data: ["jian":"\(self.jianNumber)","cr":"\(self.crNumber)","er":"\(self.erNumber)"])
        self.hide()
        self.hideKeyboard()
    }
}
